/**
 * 关于对于request的异步请求的封装
 * auther：xianghai
 * time:2020 12 31
 * 
 */
export default class Request {
	constructor(arg) {
		this.DomainH="https://wangjibu.top";
		this.Domain = "https://wangjibu.top/web/index.php/";
		// this.DomainH="http://192.168.3.227/xianghai/";
		// this.Domain = "http://192.168.3.227/xianghai/web/index.php/";
		this.resDomain = "https://wangjibu.top/web/resource/";
		this.appid = "wx5af09f4acf5b5c48";
		this.msg = "hello";
		this.sessionData = "";
		this.isLogin = false;
		//获取网络状态
		uni.getNetworkType({
			success: function(res) {
				console.log("获取网络的状态" + res.networkType);
			}
		});
		//获取登录状态
		this.updataSession();
	}
	updataSession() {
		uni.getStorage({
			key: "session",
			success: (res) => {
				if (res.data.code == 101) {
					this.isLogin = false;
					this.sessionData = false;
				} else {
					this.isLogin = true;
					this.sessionData = res;
				}

			},
			fail: (err) => {
				console.log("获取失败");
				console.log(err);
				this.isLogin = false;
				this.sessionData = false;
			}
		});

	}
	showToast(content) {
		uni.hideLoading();
		uni.showToast({
			title: content,
			image: false
		});

	}
	tishi() {
		uni.showModal({
			title: "提示",
			content: "网络似乎断开了",
			showCancel: false,
			success: (e) => {
				console.log(e);
			}
		})
	}
	/**
	 * 日期转换时间时间戳转换时间
	 * @param {Object} time
	 */
	to_date(time, type) {
		let str = parseInt(time)*1000;
		let newDate = new Date(str);
		let year = newDate.getUTCFullYear();
		let month = newDate.getUTCMonth();
		let nowday = newDate.getUTCDate();
		let hours = newDate.getHours(); //取小时
		let minutes = newDate.getMinutes(); //取分钟
		let seconds = newDate.getSeconds(); //取秒
		hours = (hours < 10) ? "0" + hours : hours;
		minutes = (minutes < 10) ? "0" + minutes : minutes;
		let typeArr = {
			0: year + " " + month + "." + nowday + "" + " " + hours + ":" + minutes,
			1: year + "." + month + "." + nowday,
		}

		return typeArr[type];

	}
	init() {
		console.log("成功调用" + this.msg);
	}
	/**
	 * 判断是否登录，未登录直接跳转到登录授权界面
	 */
	isLogin(callback) {
		if (!this.loginStatus) {
			uni.navigateTo({
				url: "/pages/login/login",
				animationType: 'slide-in-bottom'
			});
			callback(false);
		} else {
			callback(true);
		}
	}
	/**
	 * 检查登录
	 * @param {Object} callback
	 */
	checkLogin(callback) {
		console.log("sessionData " + this.sessionData);
		uni.getStorage({
			key: "session",
			success: (res) => {
				if (res.data.code == 101) {
					callback(false);
				} else {
					this.isLogin = true;
					this.sessionData = res;
					callback(true);
				}

			},
			fail: (err) => {
				callback(false);
			}
		});
	}
	/**
	 * 重新登录的操作
	 * @param {Object} callback
	 */
	reLogin() {
		uni.showLoading({
			title: "请求中..."
		})
		return new Promise((resolve, reject) => {
			let msg = {
				code: "",
				msg: ""
			}
			uni.getProvider({
				service: 'oauth',
				success: (res) => {
					if (~res.provider.indexOf('weixin')) {
						uni.login({
							provider: 'weixin',
							success: (loginRes) => {
								let appid = this.appid; //appid
								let url = "xcx/weixin.api.php";
								let data = {
									"action": "getWeixinUserInfoByCode",
									"appId": appid,
									"code": loginRes.code
								};
								//请求接口
								this.request(url, 'GET', data).then((res) => {
									console.log(res);
									if (res.statusCode == 200 && res.data.code == 0) {
										uni.setStorage({
											key: "session",
											data: res.data,
											success: () => {
												msg.code = 0;
												msg.msg = "登录成功";
												this.updataSession();
												resolve(msg);
												console.log("stroage success!");
											},

										});
									} else {
										msg.code = 101;
										msg.msg = "登录失败";
										reject(msg);
							
									}

								}, (err) => {
									msg.code = 101;
									msg.msg = "登录失败";
									reject(msg);
									console.log("获取失败");

								});



							}
						});
					} else {
						msg.code = 101;
						msg.msg = "登录失败";
						reject(msg);
						uni.showModal({
							title: '提示',
							confirmText: '支持微信小程序'
						});
						console.log("只支持微信");

					}
				},
				fail: () => {
					msg.code = 101;
					msg.msg = "登录失败";
					reject(msg);
				}

			});





		})


	}




	/**
	 * 模拟异步请求
	 */
	getDate(callback) {
		let status = false,
			time = 1;
		let timer = setInterval(function() {
			if (time == 5) {
				status = true;
				callback(status);
				clearInterval(timer);
			}
			time++;
			console.log("status " + status + " " + time);
		}, 1000);
	}

	/**
	 * 关于异步请求的封装
	 * @param {Object} url 请求的地址
	 * @param {Object} type 请求的类型
	 * @param {Object} data  请求的参数
	 */
	request(url, type, data) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: this.Domain + url,
				method: type,
				data: data,
				header: {
					'Content-Type': 'application/json'
				},
				success: res => {
					resolve(res);
				},
				fail: (e) => {
					reject(e);
				},
				complete: () => {}
			});

		});

	}
	/**
	 * 普通接口的数据请求
	 * 无须处于登录状态
	 * @param {Object} url
	 * @param {Object} type
	 * @param {Object} data
	 */
	commonQuest(url, type, data) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: this.Domain + url,
				method: type,
				data: data,
				header: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				success: (res) => {
					resolve(res);
				},
				fail: (e) => {
					reject(e);
				}
			})
		})
	}


	/**支付请求的封装
	 * @param {Object} url
	 * @param {Object} type
	 * @param {Object} data
	 * 必须处于登录状态
	 */
	payment(url, type, data) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: url,
				method: type,
				data: data,
				header: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				success: (res) => {
					resolve(res);
				},
				fail: (e) => {
					reject(e);
				}
			})
		});

	}
	
	
	testUtil(){
		
		
		return new Promise((resolve, reject) => {
			uni.request({
				url: "http://192.168.3.227/changgecms/index/api/testUtil.html",
				method: 'post',
				dataType:'json',
				data: {
					msg:"客户端发送的消息"
				},
				header: {
					'Content-Type': 'application/json'
				},
				success: (res) => {
					resolve(res);
				},
				fail: (e) => {
					reject(e);
				}
			})
		});
	}





}
